package artid.com.arttogo.activity;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;

import artid.com.arttogo.R;
import artid.com.arttogo.bean.OneStateIntParser;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.ToastUtil;

/**
 * 
 *    
 * 项目名称：ArtworkArt   
 * 类名称：ReportActivity   
 * 类描述：选择举报原因   
 * 创建人：耿卫斌   
 * 创建时间：2017-1-6 上午10:44:44   
 *
 */
public class ReportActivity extends BaseActivity implements OnClickListener {
	private Context mContext;
	private String id="1164";//评论ID
	private String type;//举报类型
	private String inform_type="3";//接口举报类型
	private TextView titleRight,Law_tv,Pornographic_tv,copyright_tv,other_tv;
	private EditText contentEd;
	private int intentType;//从什么地方进入的（默认为-1postData（）方法。-2为拍卖行举报postAuctionData()方法）

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_report);
		mContext=ReportActivity.this;
		id = getIntent().getExtras().getString("id");
		if (getIntent().getExtras().getString("type")!=null) {
			inform_type = getIntent().getExtras().getString("type");
		}
		intentType=getIntent().getIntExtra("intentType", -1);
		initView();
	}
	private void initView(){
		mBack = (ImageView) findViewById(R.id.back_btn);
		mBack.setVisibility(View.VISIBLE);
		mBack.setOnClickListener(this);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		mTittle.setText("选择举报原因");
		titleRight=(TextView) findViewById(R.id.tittle_text_info);
		titleRight.setEnabled(false);
		titleRight.setTextColor(Color.parseColor("#D0D0D0"));
		titleRight.setText("提交");
		titleRight.setVisibility(View.VISIBLE);
		titleRight.setOnClickListener(this);
		Law_tv=(TextView) findViewById(R.id.Report_Law);
		Law_tv.setOnClickListener(this);
		Pornographic_tv=(TextView) findViewById(R.id.Report_Pornographic);
		Pornographic_tv.setOnClickListener(this);
		copyright_tv=(TextView) findViewById(R.id.Report_copyright);
		copyright_tv.setOnClickListener(this);
		other_tv=(TextView) findViewById(R.id.Report_other);
		other_tv.setOnClickListener(this);
		contentEd=(EditText) findViewById(R.id.Report_contentEd);
		contentEd.addTextChangedListener(mTextChangedListener);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_btn:
			//返回
			finish();
			break;
		case R.id.tittle_text_info:
			//提交
			if(intentType==-1){
				postData();
			}else if(intentType==-2){
				postAuctionData();
			}
			break;
		case R.id.Report_Law:
			//法律
			type="1";
			setDrawableLeft(Law_tv);
			ConditionJudgment();
			break;
		case R.id.Report_Pornographic:
			//色情
			type="2";
			setDrawableLeft(Pornographic_tv);
			ConditionJudgment();
			break;
		case R.id.Report_copyright:
			//版权
			type="3";
			setDrawableLeft(copyright_tv);
			ConditionJudgment();
			break;
		case R.id.Report_other:
			//其他
			type="4";
			setDrawableLeft(other_tv);
			ConditionJudgment();
			break;

		default:
			break;
		}
	}
	public void setDrawableLeft(TextView v){
		Drawable drawable = getResources().getDrawable(R.drawable.ss_ico_dui);
		drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); //设置边界
		Law_tv.setCompoundDrawables(null, null, null, null);//画在右边
		Pornographic_tv.setCompoundDrawables(null, null, null, null);//画在右边
		copyright_tv.setCompoundDrawables(null, null, null, null);//画在右边
		other_tv.setCompoundDrawables(null, null, null, null);//画在右边
		v.setCompoundDrawables(null, null, drawable, null);//画在右边
	}


	/**
	 * 对EditText监听
	 */
	TextWatcher mTextChangedListener = new TextWatcher() {
		private CharSequence temp;
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub  
			temp = s;  
		}  

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
									  int after) {
			// TODO Auto-generated method stub  
			//		          mTextView.setText(s);//将输入的内容实时显示  
		}  

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub   
			//对按钮的判断是否可用
			ConditionJudgment();
		}  
	};  

	/**
	 * 条件判断
	 */
	private Boolean ConditionJudgment(){
		if("1".equals(type) || "2".equals(type) || "3".equals(type)
				|| ("4".equals(type) && !TextUtils.isEmpty(contentEd.getText().toString()))){
			titleRight.setEnabled(true);
			titleRight.setTextColor(Color.parseColor("#90AD72"));
			return true;
		}else{
			titleRight.setEnabled(false);
			titleRight.setTextColor(Color.parseColor("#D0D0D0"));
			return false;
		}
	}


	/**
	 * 提交投诉信息
	 * 评论ID	id
		举报内容	content
		举报类型	type 1.违背法律 2.色情暴力 3.内容造假 4.其他原因
		接口举报类型 inform_type	1.艺术品 2.资讯 3.评论
	 */
	private void postData(){
		VolleyBasic.getInstance(this).post(Cache.report,
				VolleyMap.getInstance().report(id, contentEd.getText().toString(), type,inform_type),
				OneStateIntParser.class, new VolleyCallback() {

			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				OneStateIntParser mBean  = (OneStateIntParser)vd.getData();
				if (mBean.getState()==1) {
					ToastUtil.toast(mContext, "举报成功");
					finish();
				}else{
					ToastUtil.toast(mContext, "举报失败");
				}
			}

			@Override
			public void onError(VolleyError volleyError) {
				// TODO Auto-generated method stub
			}
		});
	}
	
	/**
	 * 举报拍卖行
		举报ID	contentid	
		举报描述	content	
		举报内容类型	type	举报内容所属类型1.违背法律 2.色情暴力 3.内容造假 4.其他原因
		举报类型	inform_type	1,机构，2，拍卖会，3专场，4拍品,5评论
	 */
	private void postAuctionData(){
		VolleyBasic.getInstance(this).post(Cache.userInform, 
				VolleyMap.getInstance().userInform(id, contentEd.getText().toString(), type, inform_type), 
				OneStateIntParser.class, new VolleyCallback() {
			
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				OneStateIntParser mBean  = (OneStateIntParser)vd.getData();
				if (mBean.getState()==1) {
					ToastUtil.toast(mContext, "举报成功");
					finish();
				}else{
					ToastUtil.toast(mContext, "举报失败");
				}
			}
			
			@Override
			public void onError(VolleyError volleyError) {
				// TODO Auto-generated method stub
			}
		});
	}

}
